package com.cloud.pole.weixin.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.weixin.biz.WxAppBiz;
import com.cloud.pole.weixin.model.entity.WxApp;
import com.cloud.pole.weixin.model.entity.WxUser;
import com.cloud.pole.weixin.model.vo.Code2SessionVo;
import com.cloud.pole.weixin.service.MinProgramService;
import com.cloud.pole.weixin.service.WechatApiService;
import com.cloud.pole.weixin.util.wechat.SHA1;
import com.cloud.pole.weixin.util.wechat.WxCommonConstants;
import com.cloud.pole.weixin.wechat.domain.BaseRespMsg;
import com.cloud.pole.weixin.wechat.handler.DefaultSession;
import com.cloud.pole.weixin.wechat.handler.HandleMessageAdapter;
import com.cloud.pole.weixin.wechat.message.Msg;
import com.cloud.pole.weixin.wechat.message.Msg4Event;
import com.cloud.pole.weixin.wechat.message.Msg4Text;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("minProgram/front")
@Slf4j
public class MinProgramController {

	@Autowired
	private MinProgramService minProgramService;
	
	@ResponseBody
    @RequestMapping(value = "/code2Session")
    public ApiRestResponse<Code2SessionVo> code2Session( 		
    		@RequestParam(defaultValue = "") String wxAppId, 
    		@RequestParam(defaultValue = "") String jsCode){   	
    	try {
			ParamException.err(StringUtils.isBlank(wxAppId), "wxAppId不能为空");	
			ParamException.err(StringUtils.isBlank(jsCode), "jsCode不能为空");			
			ParamException.err(!RegexUtils.isPositiveInteger(wxAppId), "wxAppId错误");	
    		return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, minProgramService.code2Session(Integer.parseInt(wxAppId), jsCode));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	

	/**
	 * 小程序保存用户资料
	 * @param wxAppId
	 * @param openId
	 * @param nickName
	 * @param avatarUrl
	 * @param gender
	 * @param country
	 * @param province
	 * @param city
	 * @param language
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/addUserInfo")
    public ApiRestResponse<String>  addUserInfo(
    		@RequestParam(defaultValue = "") String wxAppId, 
    		@RequestParam(defaultValue = "") String openId,
    		@RequestParam(defaultValue = "") String nickName,
    		@RequestParam(defaultValue = "") String avatarUrl,
    		@RequestParam(defaultValue = "") String gender,
    		@RequestParam(defaultValue = "") String country, 
    		@RequestParam(defaultValue = "") String province,
    		@RequestParam(defaultValue = "") String city,
    		@RequestParam(defaultValue = "") String language
    		){   	
    	try {
    		ParamException.err(StringUtils.isBlank(wxAppId), "wxAppId不能为空");	
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");			
			ParamException.err(!RegexUtils.isPositiveInteger(wxAppId), "wxAppId错误");	
			ParamException.err(!RegexUtils.isInteger(gender), "gender错误");	

			minProgramService.addUserInfo(Integer.parseInt(wxAppId), openId, nickName, avatarUrl, Integer.parseInt(gender), country, province, city, language);
    		return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
    		
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	/**
	 * 小程序解密数据
	 * @param openId
	 * @param encryptedData
	 * @param iv
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/decryptData")
    public ApiRestResponse<String>  decryptData(
    		@RequestParam(defaultValue = "") String openId, 
    		@RequestParam(defaultValue = "") String encryptedData, 
    		@RequestParam(defaultValue = "") String iv){   	
    	try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");			
			ParamException.err(StringUtils.isBlank(encryptedData), "encryptedData不能为空");			
			ParamException.err(StringUtils.isBlank(iv), "iv不能为空");			

    		return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, minProgramService.decryptData(openId, encryptedData, iv));
    		
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	@ResponseBody
    @RequestMapping(value = "/getWxQrCodeUnLimit")
    public ApiRestResponse<String>  getWxQrCodeUnlimit(
    		@RequestParam(defaultValue = "") String wxAppId,
    		@RequestParam(defaultValue = "") String scene,
    		@RequestParam(defaultValue = "") String page)
    		{   	
    	try {
			ParamException.err(StringUtils.isBlank(scene), "scene不能为空");		
			ParamException.err(StringUtils.isBlank(page), "page不能为空");		
			
    		return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, minProgramService.getWxQrCodeUnLimit(Integer.parseInt(wxAppId), scene, page));
    		
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
}
